################################################################################
################################################################################
rslt.file 			= paste(c(rslt.path,"r-cb-estimated.txt"),collapse = "")

population.list 	= c("Full","Treated","Untreated")
file.list 			= c("full","marginal","conditional")
estimand.list  	= c("average-benefit-ex-post","average-benefit",
							 "average-cost","average-surplus")
#-------------------------------------------------------------------------------
#prepare output
#-------------------------------------------------------------------------------
cb.estimated = array(dim = c(3, 12,4))
for(e in 1:3){
	lower = 12*(e - 1) + 1; upper = 12*e
   cb.estimated[e,,] = as.matrix(read.table(rslt.file, na.strings = ".")[lower:upper,])
}
cb.estimated[,,c(1,3,4)] =  cb.estimated[,,c(1,3,4)]/4
#-------------------------------------------------------------------------------
#prepare tables
#-------------------------------------------------------------------------------
for(r in 1:2){; for(e in 1:3){
	#----------------------------------------------------------------------------
	#full lists
	#----------------------------------------------------------------------------
	if(r == 1){
		file.path   = paste(c(out.path,"t-cb-",file.list[e],".tex"), collapse = "")
		text = 	paste(c("\\begin{tabular}{|r||r@{}l|r|r@{ / }r|}\\hline\\hline"))
		write(text, file = file.path, append = FALSE, sep = " ")
	}; g = 0
for(i in 1:4){
	#----------------------------------------------------------------------------
	#partial lists
	#----------------------------------------------------------------------------
	if(r == 2){
		file.path   = paste(c(out.path,"t-cb-",file.list[e],"-",estimand.list[i],".tex"), collapse = "")
		text = 	paste(c("\\begin{tabular}{|r||r@{}l|r|r@{ / }r|}\\hline\\hline"))
		write(text, file = file.path, append = FALSE, sep = " ")
	}

	if(i == 1) text = paste(c("\\mc{6}{|c|}{Average Benefit of Treatment (Ex - Post)}\\\\\\hline\\hline"), collapse = "")
	if(i == 2) text = paste(c("\\mc{6}{|c|}{Average Benefit of Treatment}\\\\\\hline\\hline"), collapse = "")
	if(i == 3) text = paste(c("\\mc{6}{|c|}{Average Cost of Treatment}\\\\\\hline\\hline"), collapse = "")
	if(i == 4) text = paste(c("\\mc{6}{|c|}{Average Surplus of Treatment}\\\\\\hline\\hline"), collapse = "")
	write(text, file = file.path, append = TRUE, sep = "")
   text = paste(c("Population & \\mc{2}{c|}{Estimand} & p-value & \\mc{2}{c|}{90\\% Confi.}\\\\\\hline" ), collapse = "")
	write(text, file = file.path, append = TRUE, sep = "")
	for(j in 1:3){
		g = g + 1	   	

		 p.stars = c("$^{}$")
		if(cb.estimated[e,g,2] <= 0.10) p.stars = c("$^{*}$")
		if(cb.estimated[e,g,2] <= 0.05) p.stars = c("$^{**}$")
		if(cb.estimated[e,g,2] <= 0.01) p.stars = c("$^{***}$")

		text = paste("\\mc{1}{|l||}{",population.list[j],"}","&",
			sprintf("%15.3f", cb.estimated[e,g,1]), "&", p.stars,"&",
			sprintf("%15.2f", cb.estimated[e,g,2]),"&",
			sprintf("%15.3f", cb.estimated[e,g,3]),"&",
			sprintf("%15.3f", cb.estimated[e,g,4]),"\\\\", collapse = "")
		write(text, file = file.path, append = TRUE, sep = "")
	}
	text = c("\\hline\\hline")
	write(text, file = file.path, append = TRUE, sep = "")
}

	text = c("\\end{tabular}")
	write(text, file = file.path, append = TRUE, sep = "")
}
}
#-------------------------------------------------------------------------------
#compare the effects of treatment
#-------------------------------------------------------------------------------
p.stars  = vector("numeric", length = 6); idx = array(dim = c(6,2))
idx[1,] = c(1, 4); idx[2,] = c(2, 5); idx[3,] = c(3, 6)
idx[4,] = c(7,10); idx[5,] = c(8,11); idx[6,] = c(9,12)

file.path = paste(c(out.path,"t-cb-comparison-short.tex"), collapse="")
text 		 = paste(c("\\begin{tabular}{|r||r@{}l|r@{}l|r@{}l||r||r@{}l|r@{}l|r@{}l|}\\hline\\hline"))
write(text, file = file.path, append = FALSE, sep = " ")

g  = 0
for(i in 1:2){
	if(i == 1) text = paste(c("\\mc{7}{|c||}{Average Benefit of Treatment (ex - post)} & \\mc{7}{c|}{Average Benefit of Treatment (ex-ante)}\\\\\\hline\\hline"), collapse = "")
	if(i == 2) text = paste(c("\\mc{7}{|c||}{Average Cost of Treatment}                & \\mc{7}{c|}{Average Surplus of Treatment}\\\\\\hline\\hline"), collapse = "")

	write(text, file = file.path, append = TRUE, sep = "")
   text = paste(c("Population & \\mc{2}{c|}{Full} & \\mc{2}{c|}{Marginal} & \\mc{2}{c||}{Conditional}  & 
						 Population & \\mc{2}{c|}{Full} & \\mc{2}{c|}{Marginal} & \\mc{2}{c|}{Conditional}\\\\\\hline" ), collapse = "")
	write(text, file = file.path, append = TRUE, sep = "")

	for(j in 1:3){ 
    	g = g + 1
		for(e in 1:3){
			p.stars[e] = c("$^{}$"); p.stars[e + 3] = c("$^{}$")
			if(cb.estimated[e,idx[g,1],2] <= 0.10) p.stars[e] = c("$^{*}$")
			if(cb.estimated[e,idx[g,1],2] <= 0.05) p.stars[e] = c("$^{**}$")
			if(cb.estimated[e,idx[g,1],2] <= 0.01) p.stars[e] = c("$^{***}$")

			if(cb.estimated[e,idx[g,2],2] <= 0.10) p.stars[3 + e] = c("$^{*}$")
			if(cb.estimated[e,idx[g,2],2] <= 0.05) p.stars[3 + e] = c("$^{**}$")
			if(cb.estimated[e,idx[g,2],2] <= 0.01) p.stars[3 + e] = c("$^{***}$")
		}

		text = paste("\\mc{1}{|l||}{",population.list[j],"}","&",
		   sprintf("%15.3f", cb.estimated[1,idx[g,1],1]), "&", p.stars[1],"&",
			sprintf("%15.3f", cb.estimated[2,idx[g,1],1]), "&", p.stars[2],"&",
			sprintf("%15.3f", cb.estimated[3,idx[g,1],1]), "&", p.stars[3],"&",
			"\\mc{1}{l||}{",population.list[j],"}","&",
			sprintf("%15.3f", cb.estimated[1,idx[g,2],1]), "&", p.stars[4],"&",
			sprintf("%15.3f", cb.estimated[2,idx[g,2],1]), "&", p.stars[5],"&",
			sprintf("%15.3f", cb.estimated[3,idx[g,2],1]), "&", p.stars[6],"\\\\", collapse = "")
		
		write(text, file = file.path, append = TRUE, sep = "")
}
	text = c("\\hline\\hline")
	write(text, file = file.path, append = TRUE, sep = "")
}
text = c("\\end{tabular}")
write(text, file = file.path, append = TRUE, sep = "")
#-------------------------------------------------------------------------------
#compare the effects of treatment
#-------------------------------------------------------------------------------
p.stars   = vector("numeric", length = 3)
file.path = paste(c(out.path,"t-cb-comparison-long.tex"), collapse="")
text      = paste(c("\\begin{tabular}{|r||r@{}l|r@{}l|r@{}l|}\\hline\\hline"))
write(text, file = file.path, append = FALSE, sep = "")

g = 0
for(i in 1:4){
	if(i == 1) text = paste(c("\\mc{7}{|c|}{Average Benefit of Treatment (Ex - Post)}\\\\\\hline\\hline"), collapse = "")
	if(i == 2) text = paste(c("\\mc{7}{|c|}{Average Benefit of Treatment}\\\\\\hline\\hline"), collapse = "")
	if(i == 3) text = paste(c("\\mc{7}{|c|}{Average Cost of Treatment}\\\\\\hline\\hline"), collapse = "")
	if(i == 4) text = paste(c("\\mc{7}{|c|}{Average Surplus of Treatment}\\\\\\hline\\hline"), collapse = "")
	write(text, file = file.path, append = TRUE, sep = "")
   text = paste(c("Population & \\mc{2}{c|}{Full} & \\mc{2}{c|}{Marginal}& \\mc{2}{c|}{Conditional}\\\\\\hline" ), collapse = "")
	write(text, file = file.path, append = TRUE, sep = " ")

	for(j in 1:3){
		g = g + 1
		for(e in 1:3){
			p.stars[e] = c("$^{}$")
			if(cb.estimated[e,g,2] <= 0.10) p.stars[e] = c("$^{*}$")
			if(cb.estimated[e,g,2] <= 0.05) p.stars[e] = c("$^{**}$")
			if(cb.estimated[e,g,2] <= 0.01) p.stars[e] = c("$^{***}$")
		}
		text = paste("\\mc{1}{|l||}{",population.list[j],"}","&",
			sprintf("%15.3f", cb.estimated[1,g,1]), "&", p.stars[1],"&",
			sprintf("%15.3f", cb.estimated[2,g,1]), "&", p.stars[2],"&",
			sprintf("%15.3f", cb.estimated[3,g,1]), "&", p.stars[3],"\\\\", collapse = "")
		write(text, file = file.path, append = TRUE, sep = "")
}
	text = c("\\hline\\hline")
	write(text, file = file.path, append = TRUE, sep = "")
}
text = c("\\end{tabular}")
write(text, file = file.path, append = TRUE, sep = "")
































